package com.project.service.IMP;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.Bean.ShopBean;
import com.project.mapper.IShopMapper;
import com.project.service.IShopService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class ShopService extends BaseService implements IShopService {
    @Override
    public void addShop(ShopBean shop) {
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        mapper.addShop(shop);
        session.commit();
        session.close();

    }

    @Override
    public void deleteShopById(Integer id) {
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        mapper.deleteShopById(id);
        session.commit();
        session.close();
    }

    @Override
    public void updateShopPriceById(Integer id, Integer price) {
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        mapper.updateShopPriceById(id, price);
        session.commit();
        session.close();
    }

    @Override
    public ShopBean findById(Integer id) {
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        ShopBean byId = mapper.findById(id);
        session.close();
        return byId;
    }

    @Override
    public PageInfo<ShopBean> findByNamePrice(String name, int minPrice, int maxPrice,int pageSize) {
        PageHelper.startPage(pageSize,4);
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        PageInfo<ShopBean> sb = PageInfo.of(mapper.findByNamePrice(name, minPrice, maxPrice, pageSize));
//        PageInfo<ShopBean> sb = PageInfo.of(mapper.findByNamePrice(name, minPrice, maxPrice, pageSize));
        session.close();

        return sb;
    }

    @Override
    public PageInfo<ShopBean> getShopListByFactoryAndDateRange(String factory, LocalDate startDate, LocalDate endDate,int pageSize) {
        PageHelper.startPage(pageSize,3);
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        PageInfo<ShopBean> of = PageInfo.of(mapper.getShopListByFactoryAndDateRange(factory, startDate, endDate, pageSize));
        return of;
    }

    @Override
    public void addShopList(List<ShopBean> shopList) {
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        mapper.addShopList(shopList);
        session.commit();
        session.close();
    }

    @Override
    public void delAll(int[] idList) {
        SqlSession session = this.getSession();
        IShopMapper mapper = session.getMapper(IShopMapper.class);
        mapper.delAll(idList);
        session.commit();
        session.close();
    }
}
